package com.js.data.system.controller;


import com.js.data.common.api.CommonResult;
import com.js.data.common.utils.SecurityUtils;
import com.js.data.system.annotation.Log;
import com.js.data.system.dto.BatchUserPermDTO;
import com.js.data.system.dto.PasswordDTO;
import com.js.data.system.dto.UserDTO;
import com.js.data.system.query.UserQuery;
import com.js.data.system.vo.UserPageVO;
import com.js.data.system.service.UserService;
import com.js.data.system.vo.UserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * <p>
 * 系统用户 前端控制器
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */

@RestController
@RequestMapping("/sys")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户分页
     *
     * @param query 查询条件
     * @return IPage<UserPageVO>
     */
    @PreAuthorize("@perm.check('system:user:list')")
    @GetMapping(value = "/user/page")
    public CommonResult<IPage<UserPageVO>> pageUser(@ModelAttribute UserQuery query) {
        return CommonResult.success(userService.pageUser(query));
    }


    /**
     * 获取用户详情
     *
     * @param id 用户uid
     * @return UserVO
     * @author wujj
     * @since 2020/6/4 17:41
     */
    @PreAuthorize("@perm.check('system:user:detail')")
    @GetMapping(value = "/user/{id}", produces = "application/json; charset=utf-8")
    public CommonResult<UserVO> getUser(@PathVariable Long id) {
        return CommonResult.success(userService.getUser(id));
    }

    /**
     * 新增用户
     *
     * @param userDTO 用户表单
     * @return void
     */
    @PreAuthorize("@perm.check('system:user:add')")
    @Log(value = "新增用户")
    @PostMapping(value = "/user")
    public CommonResult<Void> saveUser(@Validated @RequestBody UserDTO userDTO) {
        userService.saveUser(userDTO);
        return CommonResult.success();
    }

    /**
     * 修改用户
     *
     * @param id      用户id
     * @param userDTO 用户表单
     * @return void
     */
    @PreAuthorize("@perm.check('system:user:edit')")
    @Log(value = "修改用户")
    @PutMapping(value = "/user/{id}")
    public CommonResult<Void> editUser(@PathVariable Long id, @Validated @RequestBody UserDTO userDTO) {
        userService.editUser(id, userDTO);
        return CommonResult.success();
    }

    /**
     * 删除用户
     *
     * @param id 用户id
     * @return void
     */
    @PreAuthorize("@perm.check('system:user:del')")
    @Log(value = "删除用户")
    @DeleteMapping(value = "/user/{id}")
    public CommonResult<Void> deleteUser(@PathVariable Long id) {
        userService.deleteUser(id);
        return CommonResult.success();
    }

    /**
     * 启用用户
     *
     * @param id 用户id
     * @return void
     */
    @PreAuthorize("@perm.check('system:user:state')")
    @Log(value = "启用用户")
    @PutMapping(value = "/user/{id}/state")
    public CommonResult<Void> stateUser(@PathVariable Long id) {
        userService.stateUser(id);
        return CommonResult.success();
    }


    /**
     * 获取登录用户信息
     *
     * @return CommonResult
     */
    @GetMapping(value = "/user/info")
    public CommonResult<Object> getUserInfo() {
        return CommonResult.success(SecurityUtils.getCurrentUser());

    }

    /**
     * 重置密码
     *
     * @param passwordDTO 用户重置密码表单
     * @return uid
     * @author wujj
     * @since 2020/6/4 17:33
     */
    @PreAuthorize("@perm.check('system:user:password-reset')")
    @Log(value = "重置密码")
    @PutMapping(value = "/user/reset-password", produces = "application/json; charset=utf-8")
    public CommonResult<Void> resetPassword(@Valid @RequestBody PasswordDTO passwordDTO) {
        userService.resetPassword(passwordDTO);
        return CommonResult.success();
    }

    /**
     * 批量修改用户角色权限
     *
     * @param batchUserPermDTO 批量用户数据权限修改表单
     * @return void
     * @author wujj
     * @since 2020/6/5 14:26
     */
    @PreAuthorize("@perm.check('system:user:role-batch')")
    @Log(value = "批量修改用户角色权限")
    @PutMapping(value = "/user/role-perm/batch", produces = "application/json; charset=utf-8")
    public CommonResult<Void> batchUserRolePerm(@Valid @RequestBody BatchUserPermDTO batchUserPermDTO) {
        userService.batchUserRolePerm(batchUserPermDTO);
        return CommonResult.success();
    }

    /**
     * 批量修改用户部门数据权限
     *
     * @param batchUserPermDTO 批量用户数据权限修改表单
     * @return void
     * @author wujj
     * @since 2020/6/5 14:26
     */
    @PreAuthorize("@perm.check('system:user:dept-batch')")
    @Log(value = "批量修改用户部门数据权限")
    @PutMapping(value = "/user/dept-perm/batch", produces = "application/json; charset=utf-8")
    public CommonResult<Void> batchUserDeptPerm(@Valid @RequestBody BatchUserPermDTO batchUserPermDTO) {
        userService.batchUserDeptPerm(batchUserPermDTO);
        return CommonResult.success();
    }

    /**
     * 批量删除用户
     *
     * @param batchUserPermDTO 批量用户数据权限修改表单
     * @return void
     * @author wujj
     * @since 2020/6/5 14:26
     */
    @PreAuthorize("@perm.check('system:user:del-batch')")
    @Log(value = "批量删除用户")
    @DeleteMapping(value = "/user/batch", produces = "application/json; charset=utf-8")
    public CommonResult<Void> batchDeleteUser(@Valid @RequestBody BatchUserPermDTO batchUserPermDTO) {
        userService.batchDeleteUser(batchUserPermDTO);
        return CommonResult.success();
    }

}
